﻿using System;
using System.Collections.Generic;
using System.IO;
using Jayden.Dll.Web.Enums;

namespace Jayden.Dll.Web.Styles
{
	public class CssElement
	{
		private string m_Comments;
		private List<CssSelector> m_Selectors = new List<CssSelector>();
		private CssProperties m_Properties = new CssProperties();

		public CssElement(params string[] selectors)
		{
			foreach (string selector in selectors)
			{
				string[] parts = selector.Split(',');
				foreach (string part in parts)
				{
					string oneSelector = part.Trim();
					if(!string.IsNullOrWhiteSpace(oneSelector))
						m_Selectors.Add(new CssSelector(oneSelector));
				}
			}
		}

		public string Comments { get { return m_Comments; } set { m_Comments = value; } }
		public IEnumerable<CssSelector> Selectors { get { return m_Selectors; } }
		public CssProperties Properties { get { return m_Properties; } }
		public CssElement AddProperty(string key, string value) { m_Properties.Add(new CssProperty(key, value)); return this; }

		private void RenderIndentation(TextWriter writer, RenderMode mode, int indentation)
		{
			if (mode == RenderMode.Debug)
				for (int c = 0; c < indentation; c++)
					writer.Write('\t');
		}

		public void Render(TextWriter writer, RenderMode mode, int indentation)
		{
			if (m_Properties.Count > 0 && m_Selectors.Count > 0)
			{
				if (!string.IsNullOrWhiteSpace(m_Comments) && mode == RenderMode.Debug)
				{
					RenderIndentation(writer, mode, indentation);
					writer.WriteLine("/* {0} */", m_Comments);
				}
				if (mode == RenderMode.Debug)
				{
					RenderIndentation(writer, mode, indentation);
					writer.WriteLine("/*");
					foreach (CssSelector selector in m_Selectors)
					{
						RenderIndentation(writer, mode, indentation + 1);
						writer.WriteLine("{0} -> Score of {1}", selector.Selector, selector.Score);
					}
					RenderIndentation(writer, mode, indentation);
					writer.WriteLine("*/");
				}
				int count = 0;
				foreach (CssSelector selector in m_Selectors)
				{
					if (count > 0)
						writer.WriteLine(",");
					RenderIndentation(writer, mode, indentation);
					writer.Write(selector);
					count++;
				}
				writer.WriteLine(" {");
				foreach (CssProperty pair in m_Properties)
				{
					RenderIndentation(writer, mode, indentation + 1);
					writer.WriteLine("{0}: {1};", pair.Name, pair.Value);
				}
				writer.WriteLine("}");
			}
		}

		public int CheckValidity()
		{
			int version = 0;
			if (m_Selectors.Count == 1 && m_Selectors[0].Selector.StartsWith("@"))
			{
				if (m_Selectors[0].Selector == "@font-face" || m_Selectors[0].Selector == "@keyframes")
					return 3;
				else
					throw new Jayden.Dll.Core.Exceptions.Exception("'{0}' is an invalid css element.", m_Selectors[0].Selector);
			}
			else
			{
				foreach (CssSelector p in m_Selectors)
					version = Math.Max(version, p.CheckValidity());
				foreach (CssProperty p in m_Properties)
					version = Math.Max(version, p.CheckValidity());
				return version;
			}
		}
	}
}
